package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.behavior.dtos.ArticleInfoDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserService;
import com.heima.utils.common.WmThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
@RequiredArgsConstructor
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements ApUserService {

    private final ApUserMapper apUserMapper;
    private final ApUserFollowMapper apUserFollowMapper;

    @Override
    public ApUser queryUser(Integer id) {
        LambdaQueryWrapper<ApUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ApUser::getId, id);
        ApUser user = apUserMapper.selectOne(lambdaQueryWrapper);
        log.info("查询用户：{}", user);
        return user;
    }

    @Override
    public ResponseResult userFollow(UserRelationDto dto) {
        // 判断是否存在数据
        LambdaQueryWrapper<ApUserFollow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApUserFollow::getFollowId,dto.getAuthorId());
        ApUserFollow apUserFollow1 = apUserFollowMapper.selectOne(queryWrapper);
        // 关注
        if(dto.getOperation() == 0){
            if(apUserFollow1!= null){
                log.info("关注已存在");
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
            ApUserFollow apUserFollow = new ApUserFollow();
            apUserFollow.setUserId(WmThreadLocalUtil.getUser().getId());
            apUserFollow.setFollowId(dto.getAuthorId());
            apUserFollow.setCreatedTime(LocalDateTime.now());
            apUserFollowMapper.insert(apUserFollow);
            return ResponseResult.okResult("ok");
        }
        // 取消关注
        if(dto.getOperation() == 1){
            apUserFollowMapper.delete(queryWrapper);
            return ResponseResult.okResult("ok");
        }
        return null;
    }

    @Override
    public ResponseResult userFollowInfo(ArticleInfoDto dto) {
        log.info("查询用户关注信息dto{}",dto);
        // 判断是否存在数据
        LambdaQueryWrapper<ApUserFollow> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApUserFollow::getFollowId,dto.getAuthorId()).eq(ApUserFollow::getUserId,dto.getUserId());
        ApUserFollow apUserFollow1 = apUserFollowMapper.selectOne(queryWrapper);
        if(apUserFollow1 == null){
            log.info("关注不存在");
            return ResponseResult.okResult(null);
        }
        log.info("关注存在");
        return ResponseResult.okResult(true);
    }
}
